package cn.tedu.sp06.controller;

import cn.tdeu.sp01.pojo.Item;
import cn.tdeu.sp01.pojo.Order;
import cn.tdeu.sp01.pojo.User;
import cn.tdeu.sp01.web.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class RibbonController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return restTemplate.getForObject("http://item-service/{1}", JsonResult.class,orderId);
    }
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    public JsonResult<?> decreaseNumber(@PathVariable List<Item> items){
    return restTemplate.postForObject("http://item-service/decreaseNumber", items, JsonResult.class);
    }
    //用户
    @GetMapping("/user-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getUserFB")
    public JsonResult<User> getUser(@PathVariable String userId){
        return restTemplate.getForObject("http://user-service/{1}", JsonResult.class,userId);
    }

    @GetMapping("/user-service/{userId}/score")
    @HystrixCommand(fallbackMethod = "addScoreFB")
    public JsonResult<User> addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject("http://user-service/{1}/score?score={2}", JsonResult.class,userId,score);
    }
    //订单
    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject("http://order-server/{1}", JsonResult.class,orderId);
    }

    @GetMapping("order-service")
    @HystrixCommand(fallbackMethod = "saveOrderFB")
    public JsonResult<Order> saveOrder(){
        return restTemplate.getForObject("http://order-server", JsonResult.class);
    }
    ///////////////////// hystrix  降级
    public JsonResult<List<Item>> getItemsFB(@PathVariable String orderId){
        return JsonResult.err().msg("调用商品失败");
    }
    public JsonResult<?> decreaseNumberFB(@PathVariable List<Item> items){
        return JsonResult.err().msg("调用商品失败");

    }
    public JsonResult<User> getUserFB(@PathVariable String userId){
        return JsonResult.err().msg("调用用户失败");
    }
    public JsonResult<User> addScoreFB(@PathVariable Integer userId,Integer score){
        return JsonResult.err().msg("调用用户失败");
    }
    public JsonResult<Order> getOrderFB(@PathVariable String orderId){
        return JsonResult.err().msg("调用订单失败");
    }
    public JsonResult<Order> saveOrderFB(){
        return JsonResult.err().msg("调用订单失败");
    }
}
